package com.frankie.weibo;

import android.app.Activity;
import android.app.Application;

import com.frankie.weibo.miweibo.api.WeiboHttpUtil;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.sina.weibo.sdk.auth.Oauth2AccessToken;

import java.util.LinkedList;
import java.util.Queue;

/**
 * Created by Frankie on 2016/8/15.
 */

public class MiApplication extends Application {

    private Queue<Activity> mActivities;

    /**
     * 保存已打开界面
     */
    private static MiApplication miApplication;
    public static Oauth2AccessToken accessToken;

    @Override
    public void onCreate() {
        super.onCreate();
        mActivities = new LinkedList<Activity>();
        miApplication = this;

        initImageLoader();
        initHttpClient();
    }

    /**
     * 初始化图片加载器
     */
    public void initImageLoader() {
        ImageLoaderConfiguration.Builder config = new ImageLoaderConfiguration.Builder(this);
        config.threadPriority(Thread.NORM_PRIORITY - 2);
        config.threadPoolSize(15);
        config.diskCacheFileCount(5000);
        config.denyCacheImageMultipleSizesInMemory();
        config.diskCacheSize(100 * 1024 * 1024); // 100 MiB
        config.memoryCacheSize(50 * 1014 * 1024);/*手机内存设置*/
        config.tasksProcessingOrder(QueueProcessingType.LIFO);
        ImageLoader.getInstance().init(config.build());
    }

    /**
     * 初始化网络设置
     */
    private void initHttpClient(){
        WeiboHttpUtil.init();
    }

    public static MiApplication getInstance(){
        return miApplication;
    }

    public void addActivity(Activity activity){
        mActivities.offer(activity);
    }

    public void removeActivity(Activity activity){
        mActivities.remove(activity);
    }

    public void cleanActivity(){
        for(int i=0;i<mActivities.size();i++){
            mActivities.poll().finish();
        }
    }

}
